package com.easy.service.impl;

import com.easy.bean.Customer;
import com.easy.mapper.CustomerDao;
import com.easy.service.CustomerService;
import com.easy.util.Page;
import com.easy.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceimpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    @Override
    public Customer add(Customer customer) {
        // 新增数据
        customer.setPassword(PasswordUtil.getEncryption(customer.getPassword()));
        customerDao.add(customer);
        // 通过主键查询数据库中的数据
        Customer result = customerDao.one(customer.getId());
        return result;
    }

    @Override
    public int del(Integer id) {
        return customerDao.del(id);
    }

    @Override
    public Customer edit(Customer customer) {
        customerDao.edit(customer);
        Customer result = customerDao.one(customer.getId());
        return result;
    }

    @Override
    public List<Customer> all(Customer customer) {
        return customerDao.all(customer);
    }

    @Override
    public Customer one(Integer id) {
        return customerDao.one(id);
    }

    @Override
    public void page(Page page, Customer customer) {
        int count = customerDao.pageCount(customer);
        Object data = customerDao.page(page, customer);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    public Customer getByPhone(String phone) {
        return customerDao.getByPhone(phone);
    }
}
